'use strict';

module.exports = function(grunt) {
  require('load-grunt-tasks')(grunt);
  // Project configuration.
  grunt.initConfig({
    // Metadata.
    pkg: grunt.file.readJSON('package.json'),
    banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
      '<%= grunt.template.today("yyyy-mm-dd") %>\n' +
      '<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
      '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +
      ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */\n',
    // Task configuration.
    requirejs: {
      sourcemap: {
        options: {
          baseUrl: "app/scripts",
          mainConfigFile: "app/scripts/main.js",
          name: "main",
          out: "dist/main.js",
          optimize: 'uglify2',
          generateSourceMaps: true,

          useSourceUrl: false,
          preserveLicenseComments: false,
        },
        uglify2: {
          //Example of a specialized config. If you are fine
          //with the default options, no need to specify
          //any of these properties.
          output: {
            beautify: true
          },
          compress: {
            sequences: false,
            global_defs: {
              DEBUG: false
            }
          },
          warnings: true,
          mangle: false
        }
      }
    },

    clean: {
      files: ['dist']
    },

    watch: {
      options: {
        livereload: '<%=connect.options.livereload%>'
      },
      less: {
        files: ['app/**/*.less'],
        tasks: ['less', 'autoprefixer', 'csslint:lax']
      },

      image: {
        files: ['app/**/*.{png,jpg,gif}'],
        tasks: ['copy:image']
      },

      html: {
        files: ['app/**/*.html'],
        tasks: ['copy:html']
      },

      js: {
        files: ['app/**/*.js'],
        tasks: ['copy:js', 'jshint', 'karma:unit']
      },

      unitTest: {
        files: ['test/unit/**/*Spec.js'],
        tasks: ['jshint:test', 'karma:unit']
      },

      e2eTest: {
        files: ['test/e2e/**/*Spec.js'],
        tasks: ['jshint:test', 'copy:e2eHtml','karma:e2e', 'copy:html']
      }
    },

    copy: {
      all: {
        files: [{
          expand: true,
          cwd: 'app/',
          src: ['**/*'],
          dest: 'dist/',
          dot: true
        }]
      },

      css: {
        files: [{
          expand: true,
          cwd: 'app/',
          src: ['**/*.css'],
          dest: 'dist/'
        }]
      },

      image: {
        files: [{
          expand: true,
          cwd: 'app/',
          src: ['**/*.{png,jpg,gif}'],
          dest: 'dist/'
        }]
      },

      js: {
        files: [{
          expand: true,
          cwd: 'app/',
          src: ['**/*.js'],
          dest: 'dist/'
        }]
      },

      html: {
        files: [{
          expand: true,
          cwd: 'app/',
          src: ['**/*.html'],
          dest: 'dist/'
        }]
      },

      e2eHtml: {
        options: {
          processContent: function (content, srcpath) {
            console.log(srcpath);

            if (/index/.test(srcpath)) {
              return content.replace('body', 'body ng-app="AMail"');
            }
            return grunt.template.process(content);
          }
        },
        files: [{
          expand: true,
          cwd: 'app/',
          src: ['**/*.html'],
          dest: 'dist/'
        }]
      }
    },

    csslint: {
      strict: {
        options: {
          import: 2
        },
        src: ['dist/**/*.css']
      },

      lax: {
        options: {
          import: false
        },
        src: ['dist/**/*.css']
      }
    },

    cssmin: {
      release: {
        files: [{
          expand: true,
          cwd: 'dist/styles/',
          src: ['**/*.css'],
          dest: 'dist/styles/',
          ext: '.min.css'
        }]
      }
    },

    htmlmin: {
      dist: {
        options: {
          removeComments: true,
          collapseWhitespace: true
        },

        files: [{
          expand: true,
          cwd: 'dist/',
          src: ['**/*.html'],
          dest: 'dist/'
        }]
      }
    },

    imagemin: {
      dynamic: {
        files: [{
          expand: true,
          cwd: 'dist/',
          src: ['**/*.{png,jpg,gif}'],
          dest: 'dist/'
        }]
      }
    },

    less: {
      development: {
        options: {
          paths: ['app/styles']
        },

        files: [{
          expand: true,
          cwd: 'app/styles/',
          src: ['**/*.less'],
          dest: 'app/styles/',
          ext: '.css'
        }]
      }
    },

    autoprefixer: {
      options: {
        browsers: ['last 5 versions']
      },

      css_files: {
        expand: true,
        cwd: 'app/styles/',
        src: ['**/*.css'],
        dest: 'dist/styles/'
      }
    },
    concat: {
      options: {
        banner: '<%= banner %>',
        stripBanners: true
      },
      dist: {
        src: ['bower_components/requirejs/require.js', '<%= concat.dist.dest %>'],
        dest: 'dist/require.js'
      }
    },

    karma: {
      unit: {
        configFile: "karma.conf.js",
        singleRun: true
      },

      e2e: {
        configFile: "karma.e2e.conf.js",
        singleRun: true
      }
    },

    jshint: {
      gruntfile: {
        options: {
          jshintrc: '.jshintrc'
        },
        src: 'Gruntfile.js'
      },
      app: {
        options: {
          jshintrc: 'dist/.jshintrc'
        },
        src: ['dist/**/*.js']
      },
      test: {
        options: {
          jshintrc: 'test/.jshintrc'
        },
        src: ['test/**/*.js']
      }
    },

    connect: {
      options: {
        port: 8000,
        hostname: 'localhost',
        livereload: 35791,
        debug: true
      },

      livereload: {
        options: {

          open: {
            target: 'http://<%= connect.options.hostname%>:<%=connect.options.port%>/dist/views/index.html#/slide'
          },

          middleware: function(connect) {
            return require('./mockServer/server.js')(connect);
          }
        }
      }
    }
  });



  // Default task.
  grunt.registerTask("dev:build", [
    'clean',
    'copy:all',
    'jshint',
    'less',
    'autoprefixer',
    'csslint:lax'
  ]);

  grunt.registerTask("prod:build", [
    'dev:build',
    'cssmin',
    'htmlmin',
    'imagemin',
    'requirejs'
  ]);

  grunt.registerTask("serve", [
    'dev:build',
    'connect:livereload',
    'watch'
  ]);

  grunt.registerTask("test", [
    'dev:build',
    "karma"
  ]);
};